package com.wiscamp.ninechapters.problems.infrastructure.mappers.types;

import com.wiscamp.ninechapters.problems.infrastructure.po.types.ProblemTypeView;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.JpaSpecificationExecutor;
import org.springframework.data.jpa.repository.Query;
import org.springframework.stereotype.Repository;

import java.util.Collection;

/**
 * 题型仓储对象
 *
 * @author yifeng
 */
@Repository
public interface ProblemTypeViewMapper extends JpaRepository<ProblemTypeView, Long>, JpaSpecificationExecutor<ProblemTypeView> {
    /**
     * 通过代码获取问题类型
     *
     * @param typeCode
     * @return
     */
    @Query(value = "select q from ProblemTypeView q where q.typeCode = :typeCode and q.deleted = false")
    ProblemTypeView findByTypeCode(String typeCode);

    /**
     * 通过名称获取问题类型列表
     *
     * @param typeName
     * @return
     */
    @Query(value = "select q from ProblemTypeView q where q.typeName = :typeName and q.deleted = false")
    Collection<ProblemTypeView> findListByTypeName(String typeName);


    @Query(value = "select q from ProblemTypeView q where q.enabled = true and q.deleted = false")
    Collection<ProblemTypeView> findListByAvailable();

    /**
     * 分页获取题型列表
     *
     * @param spec
     * @param pageable
     * @return
     */
    Page<ProblemTypeView> findAll(Specification<ProblemTypeView> spec, Pageable pageable);


}